package team.first.dataimport;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import org.neo4j.driver.Session;
import team.first.util.DriverUtil;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: HAPPY
 * @Project_name: project
 * @Package_name: team.first.dataimport
 * @Date: 2021/12/3 16:35
 * @Description:
 */
public class UserImport {
    public static void userImport() {
        Session session = DriverUtil.getNewSession();
        try {
            List<Entity> userList = Db.use().query("select * from public.user");

            String addUser = "CREATE (:User{" +
                    "id:$id, " +
                    "nickname:$nickname," +
                    "email:$email," +
                    "password:$password," +
                    "avatar:$avatar," +
                    "username:$username" +
                    "})";
            userList.forEach(user -> {
                Map<String, Object> params = new HashMap<>();
                params.put("id", user.getInt("id"));
                params.put("nickname", user.getStr("nickname"));
                params.put("email", user.getStr("email"));
                params.put("password", user.getStr("password"));
                params.put("avatar", user.getStr("avatar"));
                params.put("username", user.getStr("username"));
                session.run(addUser, params);
            });
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DriverUtil.close(session);
        }
    }
}
